{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三大结构\n",
    "- 顺序\n",
    "- 分支\n",
    "- 循环\n",
    "- python没有go to语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分支\n",
    "- 分支基本语法\n",
    "    if 条件表达式:\n",
    "        语句1\n",
    "        语句2\n",
    "- 条件表达式就是计算结果必须为布尔值的表达式\n",
    "- 表达式后面的冒号不能少\n",
    "- 注意if后面出现的语句，如果属于if语句块，则必须同一个缩进等级\n",
    "- 条件表达式结果为TRUE执行if后面的缩进的语句块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "来，耍！\n"
     ]
    }
   ],
   "source": [
    "# if语句练习\n",
    "age = 19\n",
    "if age < 18:\n",
    "    print(\"去叫家长吧，我们不跟你玩！\")\n",
    "else:\n",
    "    print(\"来，耍！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双向分支\n",
    "- if...else...语句\n",
    "\n",
    "        if 条件表达式:\n",
    "            语句1\n",
    "            ...\n",
    "        else:\n",
    "            语句1\n",
    "            ...\n",
    "            \n",
    "- 双向分支有两个分支，当程序执行到if ... else ...语句的时候，一定会执行if或else的中的一个，也仅执行一个；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入性别：nan\n",
      "你输入的性别是nan\n",
      "来，抄十遍\n",
      "开始上课\n"
     ]
    }
   ],
   "source": [
    "#input的作用\n",
    "#1、在屏幕上输入括号内的字符串，可以使用字符串\n",
    "#2、接收用户输入的内容并返回到程序\n",
    "#3、input返回的内容一定是字符串类型\n",
    "\n",
    "gender = input(\"请输入性别：\")\n",
    "\n",
    "print(\"你输入的性别是{0}\".format(gender))\n",
    "\n",
    "if gender == \"nan\":\n",
    "    print(\"来，抄十遍\")\n",
    "else:\n",
    "        print(\"发糖喽\")\n",
    "        print(\"你是女生，特殊照顾一下\")\n",
    "\n",
    "print(\"开始上课\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = input(\"请输入学生的成绩：\")\n",
    "score = int(score)\n",
    "if score > 90:\n",
    "    print(\"A\")\n",
    "if score >= 80:\n",
    "    print(\"B\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多路分支\n",
    "- 很多分支的情况，简称多路分支\n",
    "\n",
    "        if 条件表达式:\n",
    "            语句1\n",
    "            ...\n",
    "        elif 条件表达式1:\n",
    "            语句1\n",
    "            ...\n",
    "        else:\n",
    "            语句1\n",
    "            ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if语句其它：\n",
    "- if语句可以嵌套使用，但不推荐\n",
    "- Python没有Switch-case语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环语句\n",
    "- 重复执行某些固定动作或者处理基本固定的事物\n",
    "- 分类\n",
    "    - for循环\n",
    "    - while循环\n",
    "## for循环\n",
    "- for循环\n",
    "    \n",
    "        for 变量 in 序列:\n",
    "            语句1\n",
    "            ....\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "# 列表就是一系列数字或者其他值，一般用中括号表示\n",
    "for name in ['A','B','C']:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range介绍\n",
    "- 生成一个数字序列\n",
    "- 具体范围可以设定\n",
    "- 注意，一般在python中，如果由表示数字范围的两个数，一般包含左边不包含右边\n",
    "- randint是特力，左右都包含\n",
    "- range函数在Python2和Python3中有严重的区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,11):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for-else语句\n",
    "- 当for循环结束的时候，会执行else语句\n",
    "- else语句是可选语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环之break,continue,pass\n",
    "- break:无条件结束整个循环\n",
    "- continue:无条件结束本次循环,进入下一轮循环\n",
    "- pass:表示路过,通常用来站位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 只打印到7\n",
    "for i in range(1,11):\n",
    "    print(i)\n",
    "    if(i == 7):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "结束\n"
     ]
    }
   ],
   "source": [
    "# 在1-10中,寻找所有偶数\n",
    "for i in  range(1,11):\n",
    "    if(i%2 == 1):\n",
    "        continue\n",
    "    else:\n",
    "       pass \n",
    "       print(i)\n",
    "else:\n",
    "    print(\"结束\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
